import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'model.dart';
import 'package:photo_view/photo_view.dart';

void gallery(List<GalleryModel> items, {int showIndex = 0}) {
  // Get.bottomSheet(
  //     GalleryPage(
  //       items: items,
  //       showIndex: showIndex,
  //     ),
  //     isScrollControlled: true,
  //     enterBottomSheetDuration: const Duration(milliseconds: 10));
  Get.to(
    () => GalleryPage(
      items: items,
      showIndex: showIndex,
    ),
    transition: Transition.noTransition,
  );
}

class GalleryPage extends StatelessWidget {
  GalleryPage({Key? key, required this.items, required this.showIndex})
      : super(key: key) {
    pageCtrl = PageController(initialPage: showIndex);
  }
  final List<GalleryModel> items;
  final int showIndex;
  late PageController pageCtrl;
  RxDouble _opacity = 1.0.obs;

  void back() {
    pageCtrl.dispose();
    Get.back();
  }

  Widget pageView() {
    return Stack(
      children: [
        // PageView.builder(
        //     physics: const ClampingScrollPhysics(),
        //     controller: pageCtrl,
        //     itemBuilder: (_, index) {
        //       return PhotoViewGestureDetectorScope(
        //         axis: Axis.vertical,
        //         child: PhotoView(
        //           backgroundDecoration: BoxDecoration(
        //             color: Colors.black.withAlpha(240),
        //           ),
        //           imageProvider: NetworkImage(items[index].url),
        //           heroAttributes: PhotoViewHeroAttributes(
        //             tag: items[index].id,
        //           ),
        //         ),
        //       );
        //     }),
        PhotoViewGallery.builder(
            itemCount: items.length,
            pageController: PageController(initialPage: showIndex),
            builder: (ctx, index) {
              return PhotoViewGalleryPageOptions(
                imageProvider: NetworkImage(items[index].url),
                heroAttributes: PhotoViewHeroAttributes(
                  tag: items[index].id,
                ),
              );
            }),
        Positioned(
          left: 0,
          top: SizeFit.statusBarHeight,
          child: InkWell(
            onTap: back,
            child: Container(
              width: 40.r,
              height: 40.r,
              alignment: Alignment.center,
              child: const Icon(
                Icons.close,
                color: ColorsUtil.white,
              ),
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return pageView();
  }
}
